package com.serookie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serookie.config.security.JwtTokenUtils;
import com.serookie.entity.Result;
import com.serookie.entity.SysUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serookie.mapper.SysUserMapper;
import com.serookie.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/6 20:15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{
    @Autowired
    private SysUserMapper adminMapper;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Override
    public Result login(String username, String password, HttpServletRequest request) {
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        if(userDetails==null||password==null || !passwordEncoder.matches(password,userDetails.getPassword())){
            return Result.error().message("用户名或密码不正确");
        }
        //更新用户信息
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String token = jwtTokenUtils.createToken(userDetails);
        SysUser sysUser = new SysUser();
        sysUser.setUpdateTime(new Date());
        adminMapper.update(sysUser,new QueryWrapper<SysUser>().eq("username",userDetails.getUsername()));
        Map<String,Object> maps=new HashMap<>();
        maps.put("token",token);
        maps.put("tokenHead", JwtTokenUtils.CLAIN_KEY_tokenHead);
        return Result.ok().message("登录成功").data(maps);
    }

    @Override
    public SysUser getSysUserByUsername(String username) {

        return  adminMapper.getSysUserByUsername(username);
    }


    @Override
    public List<SysUser> findAll() {
        return adminMapper.findAll();
    }

    @Override
    public Integer goodsCount() {
        return adminMapper.selectCount(null);
    }

    @Override
    public Integer maleCount() {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("sex",1);
        return adminMapper.selectCount(wrapper);
    }
}
